#!/usr/bin/env python3.12
# -*- coding: utf-8 -*-

import os

curpath = os.path.dirname(__file__)

if __name__ == "__main__":
    n = 25
    # *********************生成判断题数据**********************
    mapping = {"对": "A. 对", "错": "B. 错"}
    with open(curpath + "/pdt.txt", "r", encoding="utf-8") as f:
        pdts = f.readlines()
    with open(curpath + "/pdtda.txt", "r", encoding="utf-8") as f:
        pdtda = f.readlines()
    questionsData = {}
    for i in range(0, int(150 / n)):
        questions = []
        id = 1
        title = f"练习{i+1}"
        for j in range(i * n, (i + 1) * n):
            daan = pdtda[j].strip().split(".")[1]
            question = pdts[j].strip().split(".")[1:]
            questions.append(
                {
                    "id": id,
                    "question": question,
                    "options": ["A. 对", "B. 错"],
                    "answer": mapping[daan],
                }
            )
            # print(questions)
            id += 1
        questionsData[f"chapter{i+1}"] = {"title": title, "questions": questions}
    print("var questionsData = ", questionsData)
    with open("/dev/shm/pdt.js", "w", encoding="utf-8") as f:
        f.write("var questionsData = " + str(questionsData))

    # *********************生成选择题数据**********************
    mapping = {"A": 1, "B": 2, "C": 3, "D": 4}
    with open(curpath + "/xzt.txt", "r", encoding="utf-8") as f:
        xzts = f.read().split("\n\n")
    with open(curpath + "/xztda.txt", "r", encoding="utf-8") as f:
        xztda = f.readlines()
    questionsData = {}
    for i in range(0, int(300 / n)):
        questions = []
        id = 1
        title = f"练习{i+1}"
        for j in range(i * n, (i + 1) * n):
            row = xzts[j].split("\n")
            daan = xztda[j].strip().split(".")[1]
            question = row[0].strip().split(".")[1:]
            questions.append(
                {
                    "id": id,
                    "question": question,
                    "options": [row[1], row[2], row[3], row[4]],
                    "answer": row[mapping[daan]],
                }
            )
            id += 1
        questionsData[f"chapter{i+1}"] = {"title": title, "questions": questions}
    print("var questionsData = ", questionsData)
    with open("/dev/shm/xzt.js", "w", encoding="utf-8") as f:
        f.write("var questionsData = " + str(questionsData))

    # *********************生成判断题数据**********************
    # i = 1
    # mapping = {"对": "1", "错": "0"}
    # with open(curpath + "/pdt.txt", "r", encoding="utf-8") as f:
    #     pdts = f.readlines()
    # with open(curpath + "/pdtda.txt", "r", encoding="utf-8") as f:
    #     pdtda = f.readlines()
    # for pdt in pdts:
    #     row = pdt.strip()
    #     daan = pdtda[i - 1].strip().split(".")[1]
    #     item = {
    #         "_id": i,
    #         "题目内容": row,
    #         "选项": {},
    #         "答案": [mapping[daan]],
    #         "题目类型": "judge",
    #     }
    #     items.append(item)
    #     i += 1
    # print("data = ", items)
    # *********************生成选择题数据**********************
    # mapping = {"A": "1", "B": "2", "C": "3", "D": "4"}
    # with open(curpath + "/xzt.txt", "r", encoding="utf-8") as f:
    #     xzts = f.read().split("\n\n")
    # with open(curpath + "/xztda.txt", "r", encoding="utf-8") as f:
    #     xztda = f.readlines()
    # for xzt in xzts:
    #     row = xzt.split("\n")
    #     daan = xztda[i - 1].strip().split(".")[1]

    #     item = {
    #         "_id": i,
    #         "题目内容": row[0],
    #         "选项": {"1": row[1], "2": row[2], "3": row[3], "4": row[4]},
    #         "答案": [mapping[daan]],
    #         "题目类型": "single",
    #     }
    #     items.append(item)
    #     i += 1
    # print(items)

    # xzts = []
    # i=1
    # mapping = {"A": "1", "B": "2", "C": "3", "D": "4"}
    # conn = sqlite3.connect(os.path.join(curpath, "m.db"))
    # cur = conn.cursor()
    # cur.execute(f"select N,T,A,B,C,D,Q from xzt")
    # rows = cur.fetchall()
    # for row in rows:
    #     xzt = {
    #         "_id": i,
    #         "题目内容": row[1],
    #         "选项": {"1": row[2], "2": row[3], "3": row[4], "4": row[5]},
    #         "答案": [mapping[row[6]]],
    #         "题目类型": "single",
    #     }
    #     xzts.append(xzt)
    #     i+=1
    # cur.execute("select N,T,Q1 from pdt")
    # rows = cur.fetchall()
    # mapping = {"对": "1", "错": "0"}
    # for row in rows:
    #     pdt = {
    #         "_id": i,
    #         "题目内容": row[1],
    #         "选项": {},
    #         "答案": [mapping[row[2]]],
    #         "题目类型": "judge",
    #     }
    #     xzts.append(pdt)
    #     i+=1

    # print("data = ",xzts)
    # # print(len(xzts))
